<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<title>底部导航</title>
		<script type="text/javascript" src="../script/api.js"></script>
		<script type="text/javascript" src="../script/jquery.js"></script>
		<style>
			table {
				margin-left: 5px;
				margin-right: 5px;
				width: calc(100% - 10px);
				text-align: center;
				border: 0px;
				font: caption;
			}
			table tr {
				border: 1px solid #f4f4f4;
				padding: 5px;
			}
			table tr td {
				width: 32%;
				border: 1px solid #f4f4f4;
				padding: 5px
			}
			.tr1 {
				background-color: #f4f4f4;
			}
			.one {
				width: 100%;
				border-bottom: 1px solid #eee;
				padding: 5px;
			}
			.one_zi {
				margin-left: 10px;
				margin-top: 5px;
				font-size: 12px;
			}
			body {
				background: #FFF;
			}
		</style>
	</head>
	<body >
	<div id="pButtom">
		<input type="button" onclick="selType(1)" value="6轴传感器"/>
		<input type="button" onclick="selType(2)" value="其它传感器"/>
		</div>
		<div id="subButtom" style="display:none">
		<input type="button" onclick="scanDevice()" value="搜索设备"/>
		<!--<input type="button" onclick="stopConn()" value="断开设备"/>-->
		<input type="button" onclick="stopNotifyDevice()" value="停止设备更新"  style="display:none" id="stopNotify"/>
		</div>
		<div id="device_div"></div>
		<div id="device_uuid"></div>
		<div id="">
			读取：<span id="dl"></span>
		</div>
		<div id="device_data">
			<table cellpadding="0" cellspacing="0" border="0">
				<tr class="tr1">
					<td>X</td>
					<td>Y</td>
					<td>Z</td>
				</tr>
				<tr>
					<td id="x"></td>
					<td id="y"></td>
					<td id="z"></td>
				</tr>
				<tr>
					<td id="_x"></td>
					<td id="_y"></td>
					<td id="_z"></td>
				</tr>
				<tr class="tr1">
					<td>X+</td>
					<td>Y+</td>
					<td>Z+</td>
				</tr>
				<tr>
					<td id="xx"></td>
					<td id="yy"></td>
					<td id="zz"></td>
				</tr>
				<tr>
					<td id="_xx"></td>
					<td id="_yy"></td>
					<td id="_zz"></td>
				</tr>
			</table>
			<div id="_data"></div>
		</div>
		<script>
			var scanDeviceModule = null;
			apiready = function() {
				$("#device_data").hide();
				scanDeviceModule = api.require('scanDeviceModule');
			}
			var uuid = null
			var adds=[]
			sys_type_num=1
			function selType(typeNum){
				$("#pButtom").hide();
				$("#subButtom").show();
				sys_type_num=typeNum;
			}
			function scanDevice() {
				scanDeviceModule.scan(function(ret, err) {
					
					if (ret.status == 0) {
						if(adds.indexOf(ret.addr)==-1){
							adds.push(ret.addr)
							$("#device_div").append("<div onclick=\"connectedDevice('" + ret.addr + "')\">" + ret.addr + "</div>")
						}
					} else {
						alert(ret.msg)
					}
				});
			}

			function stopDevice() {
				
				scanDeviceModule.stop(function(ret, err) {
					alert("设备停止");
				});
			}
			function stopConn() {
			
				scanDeviceModule.stopConnect(function(ret, err) {
					$('#device_uuid').empty()
					alert("设备停止");
				});
			}

			function show(idx) {
				$('.all').hide();
				$('.div' + idx).show();
				$("#device_data").hide();
			}

			function connectedDevice(_uuid) {
				alert("连接设备:"+_uuid);
				uuid = _uuid
				var cur_param = {
					devices : []
				};
				var param = {
					addr : _uuid,
					id : "head"
				};
				cur_param.devices.push(param)
				scanDeviceModule.connect(cur_param, function(ret, err) {
				alert(JSON.stringify(ret))
					if (ret.status == 0) {
						$('#device_uuid').empty()
						for ( i = 0; i < ret.list.length; i++) {
							$api.append($api.byId("device_uuid"), "<div class='one' onclick='show(" + i + ")'>" + ret.list[i].UUID + "<div>");
							for ( j = 0; j < ret.list[i].CharaData.length; j++) {
								$api.append($api.byId("device_uuid"), "<div onclick=\"run('" + ret.list[i].UUID + "','" + ret.list[i].CharaData[j].UUID + "',"+ret.list[i].CharaData[j].rw+")\" class='one_zi all div" + i + "' style='display:none' >" + ret.list[i].CharaData[j].UUID + "<div>");
							}
						}
					} else {
						$('#device_uuid').empty()
						alert(ret.addr+"已断开连接")
					}
				});
			}
			//rw 2:r
			//	 8:w
			//	 10:rw
			//	 16:notify
			function run(uuid,charUuid,rw){
			var cur_bottons = new Array(0)	
			if(rw==10){
				cur_bottons.push('读取')
				cur_bottons.push('写入')
			}else
			if(rw==2){
				cur_bottons.push('读取')
			}else
			if(rw==8){
				cur_bottons.push('写入')
			}
			if(rw==16){
				cur_bottons.push('notify')
			}
			api.actionSheet({
			    title: '选择操作',
			    cancelTitle: '取消',
			    buttons: cur_bottons
			},function( ret, err ){
				if(cur_bottons[ret.buttonIndex-1]=='读取'){
				readDevice(uuid,charUuid)
				}
				if(cur_bottons[ret.buttonIndex-1]=='写入'){
				writeDevice(uuid,charUuid)
				}
				if(cur_bottons[ret.buttonIndex-1]=='notify'){
				startNotifyDevice(uuid,charUuid)
				$("#stopNotify").show()
				}
				if(cur_bottons[ret.buttonIndex-1]=='取消'){
				}
			});
			}
			function readDevice(_uuid,_charUuid) {

				var param = {
					device_name : "测试设备",
					uuid : _uuid,
					charUuid : _charUuid
				};
				scanDeviceModule.read(param, function(ret, err) {
					if(sys_type_num==1){
					alert("电量："+parseInt(ret.hex.substr(2), 16) + "%")
					}else{
				
				alert(JSON.stringify(ret))	}
				});
			}
			function stopNotifyDevice() {
				///sysAlert("停止更新设备信息");
				var param = {
					uuid : "0000FFF0-0000-1000-8000-00805F9B34FB",
					charUuid : "0000fff1-0000-1000-8000-00805f9b34fb"
				};
				scanDeviceModule.stopNotify(param, function(ret, err) {

					if (JSON.stringify(ret) == "{}"||ret.status==1) {
						alert('停止更新设备错误')
					} else {
						alert('已停止更新设备')
								alert(JSON.stringify(ret))
							$("#stopNotify").hide()
					}
				});
			}
			function writeDevice(_uuid,_charUuid) {
				api.prompt({
					buttons : ['确定', '取消']
				}, function(ret, err) {
					if (ret.text.length == 0) {
						alert("请输入你要改变的值")
					} else {
			
							var param = {
								device_name : "测试设备",
								uuid : _uuid,
								charUuid : _charUuid,
								writeContent : ret.text
							};
							scanDeviceModule.write(param, function(ret, err) {
								
								if(ret.status==0){
								alert("写入成功");
								}
							});
					
					}
				});
			}
			as = 0.000488
			bs = 0.07

			function startNotifyDevice(s_uuid, c_uuid) {
				var param = {
					uuid : s_uuid,
					charUuid : c_uuid,
					notifyNow : true
				};
				scanDeviceModule.notify(param, function(ret, err) {
				if(sys_type_num==1){
					$("#device_data").show();
					var hex = ret.hex.substr(2).replace(/(\w{4})(?=\w)/g, "$1" + ",").split(",")
					x_acceleration = _hex(parseInt(hex[0], 16))
					y_acceleration = _hex(parseInt(hex[1], 16))
					z_acceleration = _hex(parseInt(hex[2], 16))
					x_angular = _hex(parseInt(hex[3], 16))
					y_angular = _hex(parseInt(hex[4], 16))
					z_angular = _hex(parseInt(hex[5], 16))
					$("#xx").text(_toData(x_acceleration, as));
					$("#yy").text(_toData(y_acceleration, as));
					$("#zz").text(_toData(z_acceleration, as));
					$("#x").text(_toData(x_angular, bs));
					$("#y").text(_toData(y_angular, bs));
					$("#z").text(_toData(z_angular, bs));
					$("#_data").text(_hex(parseInt(hex[6], 16)) + "")
					}else{
					alert(JSON.stringify(ret));
					}
				});
			}

		
		

			function _hex(d) {
				if (d == 0 || d == 32768) {
					return 0;
				} else {
					return d > 32767 ? d - 65536 : d
				}
			}

			function _toData(d, x) {
				return (d * x).toFixed(4)
			}
		</script>
	</body>
</html>
